package com.dlc.commonlibrary.ble;

import android.os.Handler;
import android.os.Looper;
import java.util.ArrayList;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created by Administrator on 2017/7/19 0019.
 */

public class BleWorkerPower {

    private ReadCallback mCallback;
    private final StringBuilder mStringBuilder;
    private final AtomicInteger mReadNum;
    private final AtomicBoolean mStopped;
    private final AtomicInteger mErrorNumber;//长度小于14的数据长度
    private  int TYPE = 1;//1 代表正在读取，3 代表读取数据长度，2 代表读取手持机本身数据
    private ArrayList<String> rfidsList ;
    private int cashCount = 0;
    private final Handler mHandler;

    public interface ReadCallback {

        void onReceiveData(String data);
        void onDeviceDisconnected();
    }

    public BleWorkerPower() {
        mStringBuilder = new StringBuilder();
        mReadNum = new AtomicInteger();
        mStopped = new AtomicBoolean();
        mErrorNumber = new AtomicInteger();
        mHandler = new Handler(Looper.getMainLooper());
    }

    public void setCallback(ReadCallback callback) {
        mCallback = callback;
    }

    public void check(int power) {
        mStringBuilder.delete(0, mStringBuilder.length());
        mReadNum.set(0);
        mErrorNumber.set(0);
        mStopped.set(false);
        getData(power);
    }



    private void getData(int power){

        if (RRUA100API.getInstance().getConnectDevice()!= null){
            RRUA100API.getInstance().A100_setPower(power);
            RRUA100API.getInstance().readValue(new RRUA100API.BleCallBack() {
                @Override
                public void readDatas(String data,int type) {
                    data = data.replaceAll("\r|\n| ", "");
                   // if (data.contains("s")){
                        if (mCallback != null) {
                            final String finalData = data;
                            mHandler.post(new Runnable() {
                                @Override
                                public void run() {
                                    mCallback.onReceiveData(finalData);
                                }
                            });
                        }
                   // }
                }
            });
        } else { //跳去连接页面
            if (mCallback != null) {
                mCallback.onDeviceDisconnected();
            }
        }
    }



}
